//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.kingyea.esb.platform;

import org.apache.logging.log4j.LogManager;

import java.io.File;

public class Log4jInitializer {
    private String configLocation;

    public Log4jInitializer() {
    }

    public void init() throws Exception {
        File file;
        label41: {
            file = null;
            if (this.configLocation != null) {
                file = new File(this.configLocation);
                if (file.exists()) {
                    this.setConf(this.configLocation);
                    break label41;
                }
            }

            if (System.getProperty("log4j.configurationFile") != null) {
                file = new File(System.getProperty("log4j.configurationFile"));
                if (file.exists()) {
                    break label41;
                }
            }

            String path = System.getProperty("KingyeaESBHome") + "/etc/log4j2.properties";
            file = new File(path);
            if (file.exists()) {
                this.setConf(file.getAbsolutePath());
            } else {
                String xmlPath = System.getProperty("KingyeaESBHome") + "/etc/log4j2.xml";
                file = new File(xmlPath);
                if (file.exists()) {
                    this.setConf(file.getAbsolutePath());
                } else {
                    String jsonPath = System.getProperty("KingyeaESBHome") + "/etc/log4j2.json";
                    file = new File(jsonPath);
                    if (file.exists()) {
                        this.setConf(file.getAbsolutePath());
                    } else {
                        String yamlPath = System.getProperty("KingyeaESBHome") + "/etc/log4j2.yaml";
                        file = new File(file.getAbsolutePath());
                        if (file.exists()) {
                            this.setConf(yamlPath);
                        } else {
                            String log4jv1Path = System.getProperty("KingyeaESBHome") + "/etc/log4j.properties";
                            file = new File(log4jv1Path);
                            if (file.exists()) {
                                this.setConf(file.getAbsolutePath());
                            }
                        }
                    }
                }
            }
        }

        System.out.println("设置Log4j2系统环境变量完成.....配置文件路径：" + file != null ? file.getAbsolutePath() : "");
        LogManager.getLogger(Log4jInitializer.class).info("设置Log4j2系统环境变量完成.....");
    }

    private void setConf(String path) {
        System.setProperty("log4j.configurationFile", path);
        org.apache.logging.log4j.core.LoggerContext context = (org.apache.logging.log4j.core.LoggerContext)LogManager.getContext(false);
//        org.apache.logging.log4j.spi.LoggerContext context = (org.apache.logging.log4j.spi.LoggerContext)LogManager.getContext(false);
        context.reconfigure();
    }

    public void setConfigLocation(String configLocation) {
        this.configLocation = configLocation;
    }
}
